#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author   : Moty
# @Time     : 2024/10/18 22:39
# @File     : kc16_3B.py
# @Project  : CSPT
from utils.common.dateTime import parse_cp56time2a


def parse_3B_Trade_Record(body):
    # 解析0x3B 交易记录
    data = {
        "交易流水号": f"{body[0:32]}({body[0:32]})",
        "桩编号": f"{body[32:46]}({body[32:46]})",
        "枪号": f"{int(body[46:48], 16)}枪({body[46:48]})",
        "开始时间": f"{parse_cp56time2a(body[48:62])}({body[48:62]})",
        "结束时间": f"{parse_cp56time2a(body[62:76])}({body[62:76]})",
        "尖电价": f"{int.from_bytes(bytes.fromhex(body[76:84]), byteorder='little') / 100000}({body[76:84]})",
        "尖电量": f"{int.from_bytes(bytes.fromhex(body[84:92]), byteorder='little') / 10000}({body[84:92]})",
        "计损尖电量": f"{int.from_bytes(bytes.fromhex(body[92:100]), byteorder='little') / 10000}({body[92:100]})",
        "尖金额": f"{int.from_bytes(bytes.fromhex(body[100:108]), byteorder='little') / 10000}({body[100:108]})",
        "峰单价": f"{int.from_bytes(bytes.fromhex(body[108:116]), byteorder='little') / 100000}({body[108:116]})",
        "峰电量": f"{int.from_bytes(bytes.fromhex(body[116:124]), byteorder='little') / 10000}({body[116:124]})",
        "计损峰电量": f"{int.from_bytes(bytes.fromhex(body[124:132]), byteorder='little') / 10000}({body[124:132]})",
        "峰金额": f"{int.from_bytes(bytes.fromhex(body[132:140]), byteorder='little') / 10000}({body[132:140]})",
        "平单价": f"{int.from_bytes(bytes.fromhex(body[140:148]), byteorder='little') / 100000}({body[140:148]})",
        "平电量": f"{int.from_bytes(bytes.fromhex(body[148:156]), byteorder='little') / 10000}({body[148:156]})",
        "计损平电量": f"{int.from_bytes(bytes.fromhex(body[156:164]), byteorder='little') / 10000}({body[156:164]})",
        "平金额": f"{int.from_bytes(bytes.fromhex(body[164:172]), byteorder='little') / 10000}({body[164:172]})",
        "谷单价": f"{int.from_bytes(bytes.fromhex(body[172:180]), byteorder='little') / 100000}({body[172:180]})",
        "谷电量": f"{int.from_bytes(bytes.fromhex(body[180:188]), byteorder='little') / 10000}({body[180:188]})",
        "计损谷电量": f"{int.from_bytes(bytes.fromhex(body[188:196]), byteorder='little') / 10000}({body[188:196]})",
        "谷金额": f"{int.from_bytes(bytes.fromhex(body[196:204]), byteorder='little') / 10000}({body[196:204]})",
        "电表总起值": f"{int.from_bytes(bytes.fromhex(body[204:214]), byteorder='little') / 10000}({body[204:214]})",
        "电表总止值": f"{int.from_bytes(bytes.fromhex(body[214:224]), byteorder='little') / 10000}({body[214:224]})",
        "总电量": f"{int.from_bytes(bytes.fromhex(body[224:232]), byteorder='little') / 10000}({body[224:232]})",
        "计损总电量": f"{int.from_bytes(bytes.fromhex(body[232:240]), byteorder='little') / 10000}({body[232:240]})",
        "消费金额": f"{int.from_bytes(bytes.fromhex(body[240:248]), byteorder='little') / 10000}({body[240:248]})",
        "电动汽车唯一标识": f"{body[248:282]}({body[248:282]})",
        "交易标识": f"{trade_sign(body[282:284])}({body[282:284]})",
        "交易日期、时间 ": f"{parse_cp56time2a(body[284:298])}({body[284:298]})",
        "停止原因": f"{charging_stop_reason(body[298:300])}({body[298:300]})",
        "物理卡号": f"{int(body[300:316], 16)}({body[300:316]})",
    }
    formatted_data = ""
    for key, value in data.items():
        formatted_data += f"{key}: {value}\n"
    return formatted_data


def trade_sign(result):
    return "app 启动" if result == "01" else "卡启动" if result == "02" else "离线卡启动" if result == "04" \
        else "vin 码启动充电" if result == "05" else "未知"


def charging_stop_reason(sign):
    if sign == "40":
        return "结束充电， APP 远程停止"
    if sign == "41":
        return "结束充电， SOC 达到 100%"
    if sign == "42":
        return "结束充电，充电电量满足设定条件"
    if sign == "43":
        return "结束充电，充电金额满足设定条件"
    if sign == "44":
        return "结束充电，充电时间满足设定条件"
    if sign == "45":
        return "结束充电，手动停止充电"
    if sign in ["46", "47", "48", "49", "58", "4B", "65", "66", "67", "68", "69", "78"]:
        return f"预留"
    if sign == "4A":
        return "充电启动失败，充电桩控制系统故障"
    if sign == "4B":
        return "充电启动失败，控制导引断开"
    if sign == "4C":
        return "充电启动失败，断路器跳位"
    if sign == "4D":
        return "充电启动失败，电表通信中断"
    if sign == "4E":
        return "充电启动失败，余额不足"
    if sign == "4F":
        return "充电启动失败，充电模块故障"
    if sign == "50":
        return "充电启动失败，急停开入"
    if sign == "51":
        return "充电启动失败，防雷器异常"
    if sign == "52":
        return "充电启动失败， BMS 未就绪"
    if sign == "53":
        return "充电启动失败，温度异常"
    if sign == "54":
        return "充电启动失败，电池反接故障"
    if sign == "55":
        return "充电启动失败，电子锁异常"
    if sign == "56":
        return "充电启动失败，合闸失败"
    if sign == "57":
        return "充电启动失败，绝缘异常"
    if sign == "59":
        return "充电启动失败，接收 BMS 握手报文 BHM 超时"
    if sign == "5A":
        return "充电启动失败，接收 BMS 和车辆的辨识报文超时 BRM"
    if sign == "5B":
        return "充电启动失败，接收电池充电参数报文超时 BCP"
    if sign == "5C":
        return "充电启动失败，接收 BMS 完成充电准备报文超时 BRO AA"
    if sign == "5D":
        return "充电启动失败，接收电池充电总状态报文超时 BCS"
    if sign == "5E":
        return "充电启动失败，接收电池充电要求报文超时 BCL"
    if sign == "5F":
        return "充电启动失败，接收电池状态信息报文超时 BSM"
    if sign == "60":
        return "充电启动失败， GB2015 电池在 BHM 阶段有电压不允许充电"
    if sign == "61":
        return "充电启动失败， GB2015 辨识阶段在 BRO_AA 时候电池实际电压与 BCP 报文电池电压差距大于 5%"
    if sign == "62":
        return "充电启动失败，B2015 充电机在预充电阶段从 BRO_AA 变成BRO_00 状态"
    if sign == "63":
        return "充电启动失败，接收主机配置报文超时"
    if sign == "64":
        return "充电启动失败，充电机未准备就绪,我们没有回 CRO AA，对应老国标"
    if sign == "6A":
        return "充电异常中止，系统闭锁"
    if sign == "6B":
        return "充电异常中止，导引断开"
    if sign == "6C":
        return "充电异常中止，断路器跳位"
    if sign == "6D":
        return "充电异常中止，电表通信中断"
    if sign == "6E":
        return "充电异常中止，余额不足"
    if sign == "6F":
        return "充电异常中止，交流保护动作"
    if sign == "70":
        return "充电异常中止，直流保护动作"
    if sign == "71":
        return "充电异常中止，充电模块故障"
    if sign == "72":
        return "充电异常中止，急停开入"
    if sign == "73":
        return "充电异常中止，防雷器异常"
    if sign == "74":
        return "充电异常中止，温度异常"
    if sign == "75":
        return "充电异常中止，输出异常"
    if sign == "76":
        return "充电异常中止，充电无流"
    if sign == "77":
        return "充电异常中止，电子锁异常"
    if sign == "79":
        return "充电异常中止，总充电电压异常"
    if sign == "7A":
        return "充电异常中止，总充电电流异常"
    if sign == "7B":
        return "充电异常中止，单体充电电压异常"
    if sign == "7C":
        return "充电异常中止，电池组过温"
    if sign == "7D":
        return "充电异常中止，最高单体充电电压异常"
    if sign == "7E":
        return "充电异常中止，最高电池组过温"
    if sign == "7F":
        return "充电异常中止， BMV 单体充电电压异常"
    if sign == "80":
        return "充电异常中止， BMT 电池组过温"
    if sign == "81":
        return "充电异常中止，电池状态异常停止充电"
    if sign == "82":
        return "充电异常中止，车辆发报文禁止充电"
    if sign == "83":
        return "充电异常中止，充电桩断电"
    if sign == "84":
        return "充电异常中止，接收电池充电总状态报文超时"
    if sign == "85":
        return "充电异常中止，接收电池充电要求报文超时"
    if sign == "86":
        return "充电异常中止，接收电池状态信息报文超时"
    if sign == "87":
        return "充电异常中止，接收 BMS 中止充电报文超时"
    if sign == "88":
        return "充电异常中止，接收 BMS 充电统计报文超时"
    if sign == "89":
        return "充电异常中止，接收对侧 CCS 报文超时"
    if sign == "90":
        return "未知原因停止"
    else:
        return "未知原因"


__all__ = ['parse_3B_Trade_Record']
